package com.ruoyi.web.controller.consumerAdmin;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.consumer.domain.CCompany;
import com.ruoyi.consumer.domain.COrder;
import com.ruoyi.consumer.domain.COrderInvoice;
import com.ruoyi.consumer.service.impl.CCompanyServiceImpl;
import com.ruoyi.consumer.service.impl.COrderServiceImpl;
import com.ruoyi.consumer.utils.TableDataUtil;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lizhouwei
 * @ date : 2023/11/14
 * @description:
 */
@Api(tags = "管理员订单管理")
@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private CCompanyServiceImpl companyService;

    @Autowired
    private SysUserServiceImpl sysUserService;

    @Autowired
    private COrderServiceImpl orderService;
    //公司用户查看订单
    @ApiOperation("公司用户查看开票记录")
    @GetMapping("/list-order")
    public TableDataInfo listInvoice(COrder order) {
        Long userAdminId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userAdminId);
        if (sysUser == null) {
            new RuntimeException("管理员不存在");
        }
        //查询当前管理员所处公司
        CCompany company = new CCompany();
        company.setCompanDept(Math.toIntExact(sysUser.getDeptId()));
        List<CCompany> cCompanies = companyService.selectCCompanyList(company);
        if (cCompanies.size() == 0) {
            new RuntimeException("管理员所处公司不存在");
        }
        //通过公司id查询订单id
        order.setCompanyId(cCompanies.get(0).getCompanyId());
        List<COrder> cOrders = orderService.selectCOrderList(order);
        if (cOrders.size() == 0) {
            new RuntimeException("公司订单不存在");
        }

        return TableDataUtil.getDataTable(cOrders);
    }

}
